//
//  ShareView.m
//  ExamSecretary
//
//  Created by Hu on 16/5/23.
//  Copyright © 2016年 mingthink. All rights reserved.
//

#import "ShareView.h"

#define inviteH 160
@implementation ShareView
{
    UIView *shadowV;
    id shareImage;
    NSMutableDictionary *shareParams;
}
@synthesize inviteV;

-(instancetype)initWithFrame:(CGRect)frame
{
   self= [super initWithFrame:frame];
    if (self) {
        [self initSubViews];
    }
    return self;
}

-(void)initSubViews
{
    self.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeShareView)];
    tap.delegate=self;
    tap.numberOfTapsRequired=1;
    [self addGestureRecognizer:tap];

    inviteV=[[UIView alloc]initWithFrame:CGRectMake(0, DEVH, DEVW, inviteH)];
    inviteV.backgroundColor=[UIColor whiteColor];
    [self addSubview:inviteV];
    inviteV.sd_layout.topSpaceToView(self,DEVH).leftSpaceToView(self,0).rightSpaceToView(self,0);
    
//    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake((DEVW-50)/2, 20, 50, 20)];
//    title.text = @"分享";
//    title.font = GET_SYSTEM_FONT(TEXT_FONT_16);
//    title.textColor = GET_HEX_COLOR(TEXT_GRAY);
//    title.textAlignment = NSTextAlignmentCenter;
//    [inviteV addSubview:title];
//    
//    UIView *leftLine=[[UIView alloc]init];
//    leftLine.backgroundColor=GET_HEX_COLOR(TABLE_SEPARATOR_LIGHTGRAY);
//    [inviteV addSubview:leftLine];
//    leftLine.sd_layout.leftSpaceToView(inviteV,15).rightSpaceToView(title,15).centerYEqualToView(title).heightIs(1);
    
//    UIView *rightLine=[[UIView alloc]init];
//    rightLine.backgroundColor=GET_HEX_COLOR(TABLE_SEPARATOR_LIGHTGRAY);
//    [inviteV addSubview:rightLine];
//    rightLine.sd_layout.leftSpaceToView(title,15).rightSpaceToView(inviteV,15).centerYEqualToView(title).heightIs(1);
    
    int column = 4; //横排默认显示个数(还要根据下面的totalCount计算位置)
    
//    int Spacey = 5;  //各View之间的纵向距离
//    int XSpace = 20; //距离左右边框的距离
//    int YSpace = 15; //距离上线框架的距离
    CGFloat width=60*WIDTHSCALE;
    CGFloat height=width+23;
    CGFloat spaceX = 20*WIDTHSCALE;//各View之间的横向距离
    CGFloat spaceY = 20*WIDTHSCALE;//各View之间的纵向距离
    CGFloat marginX=(DEVW-width*column-(column-1)*spaceX)/2;//距离左右边框的距离
    CGFloat marginY=20;//距离上下边框的距离
    //int totalCount = 2;
    //int width =(DEVW - (weidthColumn - 1)*Spacex-XSpace*2)/weidthColumn; //宽
    //int height = (DEVH-60 - (heightColumn - 1)*Space-YSpace*2)/heightColumn; //高
    //int height = width+20;
    NSArray *itemArr = @[@[@"微信好友",@"share_wx"],@[@"微信朋友圈",@"share_wx_circle"]/*,@[@"QQ",@"share_qq"],@[@"QQ空间",@"share_qqkj"]*/];
    //NSArray *titleArr = @[@"微信好友",@"微信朋友圈",@"QQ",@"QQ空间"];
    UIButton *lastButton=nil;
    for (int i =0; i<itemArr.count; i++) {
        UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
        [inviteV addSubview:button];
        //button.frame =CGRectMake(marginX+(width+spaceX)*(i%column),marginY+(height+spaceY)*(i/column), width, height);
        if (itemArr.count<column) {
            CGFloat space=(DEVW-itemArr.count*width)/(itemArr.count+1);
             button.sd_layout.leftSpaceToView(inviteV,space+(width+space)*(i%column)).topSpaceToView(inviteV,marginY+(height+spaceY)*(i/column)).widthIs(width).heightIs(height);
        }else{
        button.sd_layout.leftSpaceToView(inviteV,marginX+(width+spaceX)*(i%column)).topSpaceToView(inviteV,marginY+(height+spaceY)*(i/column)).widthIs(width).heightIs(height);
        }
        button.tag=100+i;
        //button.adjustsImageWhenHighlighted=NO;
        [button setImage:[UIImage imageNamed:itemArr[i][1]] forState:UIControlStateNormal];
        [button setTitle:itemArr[i][0] forState:UIControlStateNormal];
        [button setTitleColor:GET_HEX_COLOR(TEXT_GRAY) forState:UIControlStateNormal];
        button.titleLabel.textAlignment=NSTextAlignmentCenter;
        button.titleLabel.font=GET_SYSTEM_FONT(TEXT_FONT_22*WIDTHSCALE);
        //button.titleLabe=[UIColor blackColor];
        [button addTarget:self action:@selector(shareClick:) forControlEvents:UIControlEventTouchUpInside];
      
        
        button.imageView.sd_layout.topSpaceToView(button,0).centerXEqualToView(button).widthIs(50).heightEqualToWidth();
        button.titleLabel.sd_layout.topSpaceToView(button.imageView,8).leftSpaceToView(button,0).rightSpaceToView(button,0).heightIs(15);
        if (i==itemArr.count-1) {
            lastButton=button;
        }
//        UILabel *lab= [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(button.frame) + 7, 0, 10)];
//        lab.text = itemArr[i][0];
//        lab.textAlignment = NSTextAlignmentCenter;
//        lab.textColor = GET_HEX_COLOR(TEXT_GRAY);
//        lab.font = GET_SYSTEM_FONT(TEXT_FONT_14);
//        [lab sizeToFit];
//        [inviteV addSubview:lab];
//        lab.sd_layout.centerXEqualToView(button);
    }
    [inviteV setupAutoHeightWithBottomView:lastButton bottomMargin:marginY];
    [inviteV updateLayout];
    
}

-(void)initShareOption
{
    //获取app图标
    NSDictionary *infoPlist = [[NSBundle mainBundle] infoDictionary];
    NSString *icon = [[infoPlist valueForKeyPath:@"CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles"] lastObject];
    if (self.imageUrlStr == nil || [self.imageUrlStr isEqualToString:@""]) {
        shareImage = [UIImage imageNamed:icon];
    }else {
        shareImage = [MyUtility getImageUrl:self.imageUrlStr];
    }
    NSURL *url=[NSURL URLWithString:self.urlStr];
    //创建分享参数
    shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:self.content
                                     images:shareImage //传入要分享的图片
                                        url:url
                                      title:self.title
                                       type:SSDKContentTypeAuto];
}

-(void)shareClick:(UIButton *)sender
{
    if (shareParams.count == 0) [self initShareOption];
     SSDKPlatformType type;
    switch (sender.tag) {
        case 100:
             type=SSDKPlatformSubTypeWechatSession;
            break;
        case 101:
                type=SSDKPlatformSubTypeWechatTimeline;
            break;
        case 102:
             type=SSDKPlatformSubTypeQQFriend;
            break;
        case 103:
              type=SSDKPlatformTypeSinaWeibo;
            break;
        default:
            break;
    }
    //进行分享
    [ShareSDK share:type //传入分享的平台类型
         parameters:shareParams
     onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
         switch (state) {
             case SSDKResponseStateSuccess:
             {
                 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"message:nil delegate:nil cancelButtonTitle:@"确定"otherButtonTitles:nil];
                 [alertView show];
                 [self removeShareView];
                 break;
             }
             case SSDKResponseStateFail:
             {
                 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"message:[NSString stringWithFormat:@"%@",error]delegate:nil cancelButtonTitle:@"OK"otherButtonTitles:nil, nil];
                 [alert show];
                 break;
             }
             default:
                 break;
         }
     }];
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if (touch.view==self) {
        return YES;
    }else{
        return NO;
    }
}

-(void)showInView:(UIView *)view
{
      [self initShareOption];
    [view addSubview:self];
    [UIView animateWithDuration:0.3 animations:^{
        inviteV.sd_layout.topSpaceToView(self, DEVH-inviteV.height_sd);
        [inviteV updateLayout];
        //inviteV.frame=CGRectMake(0,DEVH-inviteH, DEVW, inviteH);
    }];
}

-(void)removeShareView
{
    [UIView animateWithDuration:0.3 animations:^{
        //inviteV.frame=CGRectMake(0, DEVH, DEVW, inviteH);
        inviteV.sd_layout.topSpaceToView(self, DEVH);
        [inviteV updateLayout];
    } completion:^(BOOL finished) {
        [inviteV removeFromSuperview];
        [self removeFromSuperview];
    }];
}
@end
